#include <jni.h>
#include <cstdlib>
#include <stdexcept>
#include <string>

#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/imgproc/imgproc_c.h>

static CvScalar colors[] =
{
    {{0,0,255}},
    {{0,128,255}},
    {{0,255,255}},
    {{0,255,0}},
    {{255,128,0}},
    {{255,255,0}},
    {{255,0,0}},
    {{255,0,255}},
    {{255,255,255}}
};

extern "C"
JNIEXPORT jint JNICALL Java_com_opencvtest_opencvtest_getKeypointNum(JNIEnv* env,jobject thiz)
{
    IplImage* object = cvLoadImage( "/mnt/sdcard/sho1.jpg", CV_LOAD_IMAGE_GRAYSCALE );
    int i=0;
    for(i=0;i<2;i++){
	    const char* input_name="/mnt/sdcard/sho1.jpg";
	    const char* filename = input_name;
	    IplImage* img = cvLoadImage( filename, 1 );
	    cvSaveImage("/mnt/sdcard/dexamp318.jpg",img,0);
	    cvReleaseImage( &img );
	    cvDestroyWindow("result");
	    return i;
    }

/*    if(object != NULL){
        CvSURFParams params = cvSURFParams(500, 1);
        CvMemStorage* storage = cvCreateMemStorage(0);
        IplImage* object_color = cvCreateImage(cvGetSize(object), 8, 3);
        CvSeq *objectKeypoints = 0;
        CvSeq *objectDescriptors = 0;

        cvCvtColor( object, object_color, CV_GRAY2BGR );
        cvExtractSURF( object, 0, &objectKeypoints, &objectDescriptors, storage, params);

     for( int i = 0; i < objectKeypoints->total; i++ )
     {
     CvSURFPoint* r = (CvSURFPoint*)cvGetSeqElem( objectKeypoints, i );
     CvPoint center;
     int radius;
     center.x = cvRound(r->pt.x);
     center.y = cvRound(r->pt.y);
     radius = cvRound(r->size*1.2/9.*2);
     cvCircle( object_color, center, radius, colors[0], 1, 8, 0 );
     }

        cvSaveImage( "/mnt/sdcard/src_surf.jpg" , object_color);

        return objectKeypoints->total;

    }
*/
    return 0;
}
